package com.neuedu.bases;

import com.neuedu.configs.BaseConfig;
import com.neuedu.configs.PathConfig;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;


/**
 * @author Wujg(helpbusiness @ 126.com)
 * @date 2023/8/20
 */


public class InitBrowser {
    private static final String CHROME_DRIVER_PATH = PathConfig.path_tools+"/chromedriver.exe";

    private static final Logger logger = new Logger(InitBrowser.class.getName());



    public static ChromeDriver openBrowser() {

        ChromeDriver driver = null;
        String browserName = BaseConfig.browser_name.toLowerCase();
        logger.info("****** Init browser ******.");

        logger.info("Get browserName : " + browserName + ".");
        String url = BaseConfig.url;
        logger.info("Get url : " + url + ".");
        if (browserName == null) {
            logger.info("Please input a browser type in common_config.ini !!!");
            throw new WebDriverException();
        }
        if (browserName.equals("chrome")) {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--ignore-certificate-errors");

            System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
            driver = new ChromeDriver(options);
            logger.info("StartUp chrome browser.");
        } else {
            logger.info("Please input a correct browser type in common_config.ini !!!");
            throw new WebDriverException();
        }
        driver.get(url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10L));
        logger.info("Set global implicitly_wait for 20 seconds.");
        return driver;
    }
}
